<template>
  <div class="performance">
    <el-card class="taskList">
      <el-form class="s_form" :inline="true">
        <div class="left">
          <el-form-item>
            <div class="cardName">业绩列表</div>
          </el-form-item>
        </div>
        <div class="right">
          <el-form-item>
            <el-date-picker size="small" v-model="dataForm.time" type="datetimerange" :picker-options="pickerOptions"
              range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right"
              value-format="timestamp" @change="getDataList">
            </el-date-picker>
          </el-form-item>
        </div>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border :height="tabHeight">
        <el-table-column v-for="row in headerList" :key="row.key" :prop="row.key" :min-width="row.width"
          :label="row.label" show-overflow-tooltip header-align="center" align="center"></el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      dataListLoading: false,
      dataForm: {
        time: []
      },
      dataList: [],
      // 资料列表字段
      headerList: [{
        key: 'username',
        label: '人员姓名',
        width: '150'
      },
      {
        key: 'count',
        label: '业绩',
        width: '150'
      }],
      tabHeight: '600',
      pickerOptions: {
        shortcuts: [{
          text: '本月',
          onClick (picker) {
            let oneDayLong = 24 * 60 * 60 * 1000
            let now = new Date()
            let year = now.getFullYear()
            let monthStartDate = new Date(year, now.getMonth(), 1) // 当前月1号
            let nextMonthStartDate = new Date(year, now.getMonth() + 1, 1) // 下个月1号
            let days = (nextMonthStartDate.getTime() - monthStartDate.getTime()) / oneDayLong // 计算当前月份的天数
            let monthEndDate = new Date(year, now.getMonth(), days, 23, 59, 59)
            console.log([monthStartDate, monthEndDate])
            picker.$emit('pick', [monthStartDate, monthEndDate])
          }
        }, {
          text: '上个月',
          onClick (picker) {
            let oneDayLong = 24 * 60 * 60 * 1000
            let now = new Date()
            let year = now.getFullYear()
            let monthStartDate = new Date(year, now.getMonth() - 1, 1) // 当前月1号
            let nextMonthStartDate = new Date(year, now.getMonth(), 1) // 下个月1号
            let days = (nextMonthStartDate.getTime() - monthStartDate.getTime()) / oneDayLong // 计算当前月份的天数
            let monthEndDate = new Date(year, now.getMonth() - 1, days, 23, 59, 59)
            picker.$emit('pick', [monthStartDate, monthEndDate])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.tabHeight = this.$getClientHeight() - 300 + 'px'
    })
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据
    getDataList () {
      this.dataListLoading = true
      this.$http.get('cardeal/performance', {
        params: {
          limit: this.limit,
          page: this.curPage,
          startTime: (this.dataForm.time ? this.dataForm.time[0] : null),
          endTime: this.dataForm.time ? this.dataForm.time[1] : null
        }
      }).then(({ data: res }) => {
        let { code, msg, jsonObjectList } = res
        console.log(code, msg, jsonObjectList)
        this.dataList = []
        this.total = 0
        if (code === 0 && jsonObjectList) {
          this.dataList = jsonObjectList && jsonObjectList.length ? jsonObjectList : []
          this.dataListLoading = false
        }
        if (code !== 0) {
          this.$message.error(msg)
        }
        this.dataListLoading = false
      })
    }
  }
}
</script>

<style lang="scss">
.performance {
  padding: 20px;
  .taskList {
    width: 100%;
    min-width: 1100px;
    background-color: rgba(255, 255, 255, 1);
    margin-top: 15px;
    .s_form {
      display: flex;
      justify-content: space-between;
      .cardName {
        font-weight: bold;
        font-size: 18px;
      }
      .s_input {
        width: 100px;
      }
    }
  }
}
</style>
